import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';

import { SectionService } from '../../../services/section.service';
import { BookService } from '../../../services/book.service';

@Component({
  selector: 'app-home-list',
  templateUrl: './home-list.component.html',
  styleUrls: ['./home-list.component.css']
})
export class HomeListComponent implements OnInit {

  public sectionName: string = '';
  public sectionId: string = '';

  public sections: Array<any> = JSON.parse(localStorage.getItem('homeListSection')) || [];

  constructor(private sectionService: SectionService, private bookService: BookService, private location: Location,
    private currentRouter: ActivatedRoute, private router: Router) { }

  ngOnInit() {
    this.sectionId = this.currentRouter.snapshot.params['id'];
    this.sectionName = this.currentRouter.snapshot.params['name'];

    this.bookService.getBooksBySection(this.sectionId).subscribe(res => {
      if (res.Code == 100) {
        this.sections = res.Data;
        localStorage.setItem('homeListSection', JSON.stringify(this.sections));
      }
    });

  }

  public goBack(): void {
    this.location.back();
  }

  public goDetail(item): void {
    this.router.navigate(['./bookdetail/', item.Book.Id]);
  }

}
